package org.dashboard.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.dashboard.bean.AdSchema;
import org.dashboard.bean.JsonResponse;
import org.dashboard.service.AdSchemaService;
import org.dashboard.utils.NameCastTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dataset/schema")
public class AdSchemaController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(AdSchemaController.class);

    @Reference
    private AdSchemaService adSchemaService;

    @RequestMapping(value = "/get", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse get(HttpServletRequest request) {

        List<AdSchema> schemas = adSchemaService.selectAdSchemas();
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.listToListMap(schemas));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }

    @RequestMapping(value = "/search", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse search(HttpServletRequest request) {
        String criteria = request.getParameter("criteria");
        Integer pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));

        PageInfo pageInfo = adSchemaService.selectAdSchemasPaged(criteria, pageIndex, pageSize);

        JsonResponse jsonResponse = new JsonResponse();
        Map<String, Object> map = new HashMap<>();

        map.put("items", NameCastTools.listToListMap(pageInfo.getList()));
        map.put("count", pageInfo.getTotal());

        jsonResponse.setData(map);
        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }

    @RequestMapping(value = "/load", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse load(HttpServletRequest request) {

        String schemaId = request.getParameter("schemaId");
        String schemaCode = request.getParameter("schemaCode");

        AdSchema schema;

        if (!StringUtil.isEmpty(schemaId)) {
            schema = this.adSchemaService.selectByPrimaryKey(Integer.parseInt(schemaId));
        } else if (!StringUtil.isEmpty(schemaCode)) {
            schema = this.adSchemaService.selectAdSchemaByCode(schemaCode);
        } else {
            return JsonResponse.failed();
        }

        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(schema));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse update(HttpServletRequest request) {
        Integer schemaId = Integer.parseInt(request.getParameter("schemaId"));
        String schemaName = request.getParameter("schemaName");
        String schemaCode = request.getParameter("schemaCode");
        String schemaData = request.getParameter("schemaData");
        Integer schemaFolderId = Integer.parseInt(request.getParameter("schemaFolderId"));

        AdSchema adSchema = new AdSchema();
        adSchema.setSchemaId(schemaId);
        adSchema.setSchemaName(schemaName);
        adSchema.setSchemaCode(schemaCode);
        adSchema.setSchemaData(schemaData);
        adSchema.setSchemaFolderId(schemaFolderId);

        adSchemaService.updateByPrimaryKeySelective(adSchema);

        adSchema = adSchemaService.selectByPrimaryKey(schemaId);
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(adSchema));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/check", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse check(HttpServletRequest request) {
        Integer schemaId = Integer.parseInt(request.getParameter("schemaId"));
        String schemaCode = request.getParameter("schemaCode");

        Integer existCount = adSchemaService.checkSchemaCodeExist(schemaCode, schemaId);
        return existCount == 0 ? JsonResponse.success() : JsonResponse.failed();
    }

    @RequestMapping(value = "/add", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse add(HttpServletRequest request) {
        String schemaName = request.getParameter("schemaName");
        String schemaCode = request.getParameter("schemaCode");
        Integer folderId = Integer.parseInt(request.getParameter("folderId"));

        AdSchema adSchema = new AdSchema();
        adSchema.setSchemaName(schemaName);
        adSchema.setSchemaCode(schemaCode);
        adSchema.setSchemaFolderId(folderId);
        adSchema.setSchemaData("");

        adSchema.setSchemaId(adSchemaService.insert(adSchema));
        JsonResponse jsonResponse = new JsonResponse();
        jsonResponse.setData(NameCastTools.objectToMap(adSchema));

        jsonResponse.setCode(1);
        jsonResponse.setResult("success");
        return jsonResponse;
    }


    @RequestMapping(value = "/delete", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public JsonResponse delete(HttpServletRequest request) {
        String schemaIds = request.getParameter("schemaIds");
        JSONArray jsonArray = (JSONArray) JSON.parse(schemaIds);
        ArrayList<Integer> ids = new ArrayList<>();
        if (jsonArray != null) {
            int len = jsonArray.size();
            for (int i = 0; i < len; i++) {
                ids.add(jsonArray.getInteger(i));
            }
        }
        adSchemaService.deleteByPrimaryKeys(ids);
        return JsonResponse.success();
    }
}
